package com.zhaopin.gitlab;

import com.zhaopin.utils.FileUtil;
import com.zhaopin.utils.GitUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;

/**
 * 按行整
 */
@Slf4j
public class JustCheckExpSepForLine {


    //职位推荐名称
//    static String projectName = "project";
    //职位搜索名称
//    static String projectName = "position";
    //运营名称
    static String projectName = "operation";
    static String outputDir = "dir/gitlab/" + projectName;

    static String RPOJECT_NAME_FILE = "dir/gitlab/" + projectName + ".txt";
    static String gitFile = "/Users/zhaopin/IdeaProjects/" + projectName + "/%s";
    static String inPath = "/Users/zhaopin/IdeaProjects/" + projectName + "/%s/project/src/main/java/config";


    static String old = "\"CV_DESIRED_SALARY\"\n" +
            "\"CV_DESIRED_SALARY_MAX\"\n" +
            "\"CV_CURRENT_JOBTYPE_NEW\"\n" +
            "\"CV_DESIRED_CITY_ALL\"\n" +
            "\"CV_DESIRED_JOBTYPE\"\n" +
            "\"CV_DESIRED_EMPLOYMENT_TYPE\"\n" +
            "\"CV_WORK_CITY\"\n" +
            "\"CV_DESIRED_SALARY_MIN\"\n" +
            "\"CV_CURRENT_INDUSTRY_NEW\"\n" +
            "\"CV_FULL_INDEX_TOP\"\n" +
            "\"CV_DESIRED_INDUSTRY_NEW\"\n" +
            "\"CV_DESIRED_INDUSTRY\"\n" +
            "\"CV_DESIRED_JOB_TYPE_NEW\"\n" +
            "\"CV_DESIRED_CITY\"\n" +
            "\"CV_DESIRED_JOBTYPE_THIRDLEVEL_\n" +
            "\"CV_DESIRED_JOBTYPE_SECONDLEVEL_";
    static String separator = "\n";
    static String newValue = "\"similarJobLevelForCV_Exp5\"";

    public static void main(String[] args) throws IOException {

        List<String> projectNameList = FileUtils.readLines(new File(RPOJECT_NAME_FILE), Charset.defaultCharset());
        for (String s : projectNameList) {
            File repoDir = new File(String.format(gitFile, s));
            String inPathStr = String.format(inPath, s);
            //首先拉取远端代码
            GitUtils.gitPull(repoDir);

            File sFile = null;
            boolean flag = false;
            List<File> allFiles = FileUtil.getAllFiles(inPathStr);
//            List<File> allFiles = FileUtil.getAllFiles(String.format(gitFile, s));
            for (File singleFile : allFiles) {
                for (String s1 : old.split(separator)) {
                    List<String> list = FileUtils.readLines(singleFile, Charset.defaultCharset());
                    for (int i = 0; i < list.size(); i++) {
                        //按行取数据
                        String line = list.get(i);
                        if (line.contains(s1)) {
                            //继续判断是否包含这样的结果
                            //convertInput().key(xxxxx)
                            String converInputLine = "convertInput().key(" + s1 + ")";
                            if (s1.equals("\"CV_DESIRED_JOBTYPE_THIRDLEVEL_") || s1.equals("\"CV_DESIRED_JOBTYPE_SECONDLEVEL_")) {
                                converInputLine = "convertInput().key(" + s1;
                            }

                            if (line.contains(converInputLine) || line.startsWith("//")) {
                                continue;
                            } else {
                                flag = true;
                                FileUtils.write(new File(outputDir + "/" + projectName + "_need_update_file.txt"), singleFile.getAbsolutePath() + ":" + s1 + ":" + (i+1), Charset.defaultCharset(), true);
                                FileUtils.write(new File(outputDir + "/" + projectName + "_need_update_file.txt"), "\r\n", Charset.defaultCharset(), true);
                            }

                        }
                    }

                }

            }

            if (flag) {
                log.warn("需要修改的项目有：" + s);
                FileUtils.write(new File(outputDir + "/" + projectName + "_need_update_project.txt"), s, Charset.defaultCharset(), true);
                FileUtils.write(new File(outputDir + "/" + projectName + "_need_update_project.txt"), "\r\n", Charset.defaultCharset(), true);
            }


        }

    }


    public static String isHaveTheBeforeAllValueGetValue(File file, String spiltBeforeValue, String separator) throws IOException {

        String oldValue = FileUtils.readFileToString(file, Charset.defaultCharset());

        String[] splitOld = spiltBeforeValue.split(separator);

        StringBuilder returnValue = new StringBuilder();

        boolean flag = false;
        for (String s1 : splitOld) {
            if (oldValue.contains(s1)) {
                returnValue.append(s1 + ",");
                flag = true;
            }
        }
        if (flag) {
            return returnValue.toString();
        } else {
            return null;
        }
    }
}
